//
//  JDUserPortraitViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2025/4/25.
//

import UIKit
import ProgressHUD
import SwiftyJSON
/// 个人头像  页面
class JDUserPortraitViewController: JDBaseViewController {

    var userPortraitImageV: UIImageView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.setupNavigationTitle("个人头像")
        self.setupLeftBackItem()
        self.customImageRightBarButtonItem("portrait_more", #selector(moreAction), self)
    }
    
    @objc func moreAction() {
        print("选择照片")
        self.openAlbumAlert {[weak self] selectedType, images in
            
            
            guard let imgV = images.first else { return }
            self?.userPortraitImageV?.image = imgV
            
            self?.uploadImgV(imgV, { urlStrDic in
                let imageUrlKey = JSON(urlStrDic?.values.first as Any).stringValue
                self?.refreshImage(imageUrlKey)
            })
//            if selectedType == .photoCamera {
//                
//                
//
//            }  else if selectedType == .photoAblum {
//                // TODO: 这里需要处理
//                guard let imgV = images.first else { return }
//                self?.userPortraitImageV?.image = imgV
//                self?.uploadImgV(imgV, { urlStrDic in
////                    self?.imgTwoUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
//                    
//                })
//            }
        }
    }
    
    func setupUIElements() {
        self.userPortraitImageV = UIImageView()
        self.view.addSubview(self.userPortraitImageV!)
        self.userPortraitImageV?.snp.makeConstraints { make in
            make.leading.trailing.equalTo(self.view)
            make.centerY.equalTo(self.view)
            make.height.equalTo(self.userPortraitImageV!.snp.width)
        }
        self.userPortraitImageV?.downloadImage(JDLoginModel.default.headerImg ?? "", "userCenterPortraitPlaceholder")
    }
    
    
    func refreshImage(_ userPortrait: String) {
        
        var params = [String: Any]()
        params["headerImg"] = userPortrait
        ProgressHUD.animate()
        JDNetWorkRequest(.changeHeaderImg(params)) { responseStr in
            ProgressHUD.dismiss()
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                self.view.toast(message: "更新成功")
                JDLoginModel.default.headerImg = userPortrait
            }
        } failure: { error in
            ProgressHUD.dismiss()
            self.view.toast(message: error.localizedDescription)
        } error: { errorCode in
            ProgressHUD.dismiss()
            self.view.toast(message: "网络异常")
        }

    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
